package org.tity.commons.collection;

import java.util.HashMap;
import java.util.Map;

/**
 * 双向 Map
 * */
//TODO 待完善
public class DuplexMap<K,V>  extends HashMap<K, V>
{
	private static final long serialVersionUID = -2607217643545094443L;	
	private HashMap<V, K> vks=new HashMap<V,K>();
	/**
	 * put value
	 * @param key key
	 * @param value value
	 * */
	@Override
	public V put(K key, V value) {
		vks.put(value, key);
		return super.put(key, value);
	}
	
	/**
	 * putAll
	 * @param m  map
	 * */
	@Override
	public void putAll(Map<? extends K, ? extends V> m) {
		for (Entry<? extends K, ? extends V> e : m.entrySet()) {
			vks.put(e.getValue(), e.getKey());
		}
		super.putAll(m);
	}
	
	@Override
	public V putIfAbsent(K key, V value) {
		vks.putIfAbsent(value, key);
		return super.putIfAbsent(key, value);
	}
	
	@Override
	public V remove(Object key) {
		V value=super.remove(key);
		vks.remove(value);
		return value;
	}
	
	public K getKey(V value)
	{
		return vks.get(value);
	}
}
